<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript">
        //定义全局ws
        var ws;
        function subOpen() {
            //管道服务端地址
            var target="ws://localhost:8092/echo"
            if ('WebSocket' in window) {
                ws = new WebSocket(target);
            } else if ('MozWebSocket' in window) {
                ws = new MozWebSocket(target);
            } else {
                alert('WebSocket is not supported by this browser.');
                return;
            }
            //注册事件来接受服务端返回的消息
            ws.onmessage=function (event) {
                //返回event对象
                console.log(event);
                //取出属性
                alert(event.data)
            }
        }
        function sendMessage() {
            debugger
            var message=document.getElementById("send").value;
            ws.send(message)
            document.getElementById("send").value='';
        }
    </script>
</head>
<body>
<div>
    <button onclick="subOpen()">连接</button><br>
    <input type="text" id="send"><input type="button" value="发送" onclick="sendMessage()">
</div>
</body>
</html>